package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.pojo.TbSpecificationOptionExample;
import com.dongdongshop.service.SpecificationService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@DubboService
public class SpecificationServiceImpl implements SpecificationService {

    @Resource
    private TbSpecificationMapper specificationMapper;

    @Resource
    private TbSpecificationOptionMapper specificationOptionMapper;

    @Override
    public List<TbSpecification> specificationInfo() {
        return specificationMapper.selectByExample(null);
    }

    @Override
    public int add(TbSpecification specification, List<TbSpecificationOption> list) {
        int specification1 = specificationMapper.insert(specification);
        for (TbSpecificationOption tbSpecificationOption : list) {
            tbSpecificationOption.setSpecId(specification.getId());
            specificationOptionMapper.insert(tbSpecificationOption);
        }
        return specification1;
    }

    @Override
    public TbSpecification toUpdate(Long id) {
        return specificationMapper.selectByPrimaryKey(id);
    }

    @Override
    public int update(TbSpecification specification, List<TbSpecificationOption> list) {
        int i = specificationMapper.updateByPrimaryKeySelective(specification);
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(specification.getId());
        specificationOptionMapper.deleteByExample(example);
        for (TbSpecificationOption tbSpecificationOption : list) {
            tbSpecificationOption.setSpecId(specification.getId());
            specificationOptionMapper.insert(tbSpecificationOption);
        }
        return i;
    }

    @Override
    public int deleteId(Long id) {
        int i = specificationMapper.deleteByPrimaryKey(id);
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(id);
        specificationOptionMapper.deleteByExample(example);
        return i;
    }
}
